package com.stox.client.ui.javafx.watchlist.widget;

import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;

import com.stox.client.api.core.ClientFactory;
import com.stox.client.api.watchlist.WatchlistClient;
import com.stox.client.api.watchlist.WatchlistEntryClient;
import com.stox.client.core.model.intf.HasNavigable;
import com.stox.client.ui.javafx.core.StoxPlatform;
import com.stox.shared.i18n.MessageSource;
import com.stox.shared.model.Scrip;
import com.stox.shared.model.Watchlist;
import com.stox.shared.model.WatchlistEntry;
import com.stox.shared.model.intf.HasScrip;

public class AddToWatchlistMenu extends Menu {

	private final HasScrip hasScrip;
	private final HasNavigable<HasScrip> hasNavigable;
	private final EventHandler<ActionEvent> eventHandler = new EventHandler<ActionEvent>(){
		@Override
		public void handle(ActionEvent event) {
			MenuItem item = (MenuItem)event.getSource();
			Watchlist watchlist = (Watchlist)item.getUserData();
			
			if(null != hasNavigable){
				List<HasScrip> hasScrips = hasNavigable.getNavigable().currentAllSelection();
				for(HasScrip hasScrip : hasScrips){
					if(null != hasScrip && null != hasScrip.getScripId()){
						add(watchlist.getId(), hasScrip.getScripId());
					}
				}
			}else if(null != hasScrip){
				Scrip scrip = hasScrip.getScrip();
				if(null != scrip){
					add(watchlist.getId(), scrip.getId());
				}
			}
			
		}
	};
	
	public AddToWatchlistMenu(HasScrip hasScrip,HasNavigable<HasScrip> hasNavigable) {
		super(MessageSource.get("Add.to.Watchlist"));
		this.hasScrip = hasScrip;
		this.hasNavigable = hasNavigable;
		
		new Thread(){
			public void run() {
				List<Watchlist> watchlists = ClientFactory.getClient(WatchlistClient.class).findAll();
				Collections.sort(watchlists);
				StoxPlatform.runLater(new Runnable(){
					@Override
					public void run() {
						for(Watchlist watchlist : watchlists){
							addWatchlist(watchlist);
						}
					}
				});
			};
		}.start();
	}
	
	private void add(Long watchlistId, Long scripId){
		ClientFactory.getClient(WatchlistEntryClient.class).saveOrUpdate(new WatchlistEntry(scripId, watchlistId));
	}
	
	public void addWatchlist(Watchlist watchlist){
		MenuItem item = new MenuItem();
		item.setUserData(watchlist);
		item.setText(watchlist.getName());
		item.setOnAction(eventHandler);
		getItems().add(item);
	}
	
	public void updateWatchlist(Watchlist watchlist){
		for(MenuItem item : getItems()){
			Watchlist userObject = (Watchlist)item.getUserData();
			if(watchlist.getId().equals(userObject.getId())){
				userObject.setName(watchlist.getName().trim());
				item.setText(watchlist.getName().trim());
				break;
			}
		}
	}
	
	public void deleteWatchlist(Long id){
		ListIterator<MenuItem> iterator = getItems().listIterator();
		while(iterator.hasNext()){
			MenuItem item = iterator.next();
			Watchlist userObject = (Watchlist)item.getUserData();
			if(id.equals(userObject.getId())){
				iterator.remove();
				break;
			}
		}
	}
	
}
